Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();
var cm2=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'违约事件流水号',dataIndex:'serialNo',hidden:true},
{header:'违约事件类型',dataIndex:'eventType',width:100},
{header:'登记时间',dataIndex:'inputDate',width:100},
{header:'当前状态',dataIndex:'status',width:100}
]); 

var ds2=new Ext.data.Store({
proxy:new Ext.data.HttpProxy({
url:'obligation/listCustomerDefaultEvent.do',
method:'POST'
}),  
reader:new Ext.data.JsonReader({},[
{name:'serialNo'}, 
{name:'eventType'}, 
{name:'inputDate'},  
{name:'status'}
]) 
}); 
ds2.load({params:{start:0,limit:20}});

var eventsTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
		url:'showType.do?type=EventType',
		method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[{name:'name'}, {name:'code'}]) ,
	autoLoad:true
}); 
var classifyResultTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
		url:'showType.do?type=ClassifyResult',
		method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[{name:'name'}, {name:'code'}]) ,
	autoLoad:true

}); 
var grid2=new Ext.grid.GridPanel({ 
id:'defaultList', 
renderTo:'list',
stripeRows:true,
title:"当前客户违约事件列表", 
height:500, 
cm:cm2,
store: ds2, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds2,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id:'btnAdd'},
  '-',
 {text:'查看/编辑',handler:checkDetail},
 '-',
 {text:'删除',handler:del},
 '-',
 {text:'提交',handler:commit}
 ]              
});


//新增客户表单
addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 760, 
height: 420, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 140, 
items: 
[ { xtype: 'fieldset', 
title: '客户违约事件信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[
 {layout : "form",
     items : [{
     	xtype: 'textfield',
     	id: 'custId', 
     	name: 'custId', 
     	fieldLabel: '客户ID',
     	editable: false, 
     	readOnly: true,
     	hidden:true,
     	width: 200 
     }]
     },
 {layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield',
        	id: 'custNo', 
        	name: 'custNo', 
        	fieldLabel: '客户号',
        	editable: false, 
        	readOnly: true,
        	width: 200 
        }]
    },{ 
        columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	
        }]
   }]
},{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield',
        	id: 'custName', 
        	name: 'custName', 
        	fieldLabel: '客户名称',
        	editable: false, 
        	readOnly: true,
        	width: 200 
        	 
        }]
    },{ 
        columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'textfield',
        	id: 'creditLevelBf', 
        	name: 'creditLevelBf', 
        	fieldLabel: '违约前评级',
        	allowBlank: true,
        	width: 200
        }]
   }]
},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'combo', 
        	id : "eventType",
            store :eventsTypeStore,
            fieldLabel: '违约事件类型',
            valueField : 'code',
            displayField : 'name',
            emptyText : '请选择事件类型...',
            allowBlank : false,
            blankText:'请选择事件类型',
            typeAhead : true,
            width: 200,
            mode:'remote',  
            hiddenName:'eventType',
            editable: false,
            triggerAction : 'all',
            selectOnFocus : true 
        }]
    },{ 
    	columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'datefield',
        	id: 'eventDate', 
        	name: 'eventDate', 
        	fieldLabel: '违约发生日期',  
        	allowBlank: true, 
        	editable:false, 
        	width:200 ,
        	format : 'Y-m-d'
        }]
    }]
},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
	        xtype: 'textfield', 
			id:'capitalValue',
			name: 'capitalValue', 
			fieldLabel: ' 违约认定日本金余额(元)', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
	 	    regexText:'只能输入整数或小数！' , 
			width: 200
        }]
    },{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
            items : [{
        	xtype: 'textfield', 
			id:'interestsValue',
			name: 'interestsValue', 
			fieldLabel: '未偿清利息总额(元)', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
 		    regexText:'请输入整数或小数' , 
			width: 200
        }]
    }]
},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
	        xtype: 'textfield', 
			id:'loanLimit',
			name: 'loanLimit', 
			fieldLabel: '违约认定日授信额度(元)', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
	 	    regexText:'只能输入整数或小数！' , 
			width: 200
        }]
    },{
		columnWidth : .5, // 该列有整行中所占百分比
		layout : "form",
            items : [{
        	xtype: 'textfield', 
			id:'loanBalance',
			name: 'loanBalance', 
			fieldLabel: '违约认定日授信余额(元)', 
			regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
 		    regexText:'请输入整数或小数' , 
			width: 200
        }]
    }]
},

{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'combo', 
        	id : "classifyResult",
            store :classifyResultTypeStore,
            fieldLabel: '五级分类结果',
            valueField : 'code',
            displayField : 'name',
            emptyText : '请选择分类结果...',
            allowBlank : true,
            blankText:'请选择分类结果',
            typeAhead : true,
            width: 200,
            mode:'remote',  
            hiddenName:'classifyResult',
            editable: false,
            triggerAction : 'all',
            selectOnFocus : true 
        }]
    },{ 
    	columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'datefield',
        	id: 'classifyDate', 
        	name: 'classifyDate', 
        	fieldLabel: '五级分类日期',  
        	allowBlank: true, 
        	editable:false, 
        	width:200 ,
        	format : 'Y-m-d'
        }]
    }]
},

{layout : "form",
	items : [{
		xtype: 'textarea',
		id: 'eventReason', 
		name: 'eventReason', 
		fieldLabel: '违约认定原因', 
		emptyText: '必填', 
		blankText: '原因描述不能为空', 
		allowBlank: false,  
		maxLength:200,
		maxLengthText:'最多只允许输入200个汉字',
		height:100,
		anchor: '97%' 
	}]
},
{layout : "form",
	items : [{
		xtype: 'textarea',
		id: 'eventDesc', 
		name: 'eventDesc', 
		fieldLabel: '违约事件描述', 
		emptyText: '必填', 
		blankText: '事件描述不能为空', 
		allowBlank: false,  
		maxLength:200,
		maxLengthText:'最多只允许输入200个汉字',
		height:100,
		anchor: '97%' 
	}]
},
{xtype:'hidden',
id:'serialNo',
name:'serialNo',
allowBlank: true
}

]}]
});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '客户违约事件', 
width: 780, 
height: 500, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function addRoleFunction() {
	
		if(addForm.form.isValid()){
		addForm.form.doAction('submit', { 
			url : 'obligation/addCustDefaultEvent.do', 
			method : 'post', 
			success : function() { 
			Ext.getCmp("addWin").hide();
			grid2.store.reload();
			Ext.Msg.alert('提示', '保存成功！'); 
			}, 
			failure : function() { 
			Ext.Msg.alert('提示', '保存失败！'); 
			} 
		});  
	}
 }

//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addWin").hide(); 

Ext.Ajax.request({
	  url:'obligation/findNewestCDE.do?serialNo='+custId,
	  method:'POST',
	  success:function(response){
		  var addJSON = Ext.util.JSON.decode(response.responseText);
		 // alert(addJSON.status);
		  var status;
		  if(addJSON!=null)status = addJSON.status;
		  
		  if(status == null || status=="04"){
			  Ext.Ajax.request({
				  url:'obligation/getCustomer.do',
				  method:'GET',
				  success:function(response){
					var json = Ext.util.JSON.decode(response.responseText);
					Ext.getCmp('custId').setValue(json.custId);
					Ext.getCmp('custNo').setValue(json.custNo);
					Ext.getCmp('custName').setValue(json.custName);
				  },failure:function(){
				    Ext.MessageBox.alert("客户信息引入失败！");
				  }
				});  
				Ext.getCmp("addWin").buttons[0].handler = addRoleFunction; 
				Ext.getCmp("addWin").show();
			  }else{
				  Ext.Msg.alert("提示", "当前违约登记正在进行中！<br/>请在该认定结束后重新登记！");
			}
		  },failure:function(){
	    Ext.MessageBox.alert("请求异常！");
	  }
});  

 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除客户
function del(){
if (grid2.getSelectionModel().getSelections()[0] == undefined) { 
	Ext.Msg.alert("提示", "请选中要删除的内容"); 
} 
else{
	 var status =  grid2.getSelectionModel().getSelections()[0].data.status;     
	if(status == '待认定状态' ){
		Ext.Msg.alert("提示", "当前违约事件已提交，不能删除！");
	}
	else if(status == '已认定' ){
		Ext.Msg.alert("提示", "当前违约事件已认定，不能删除！");
	}
	else {Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);}
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
 Ext.Ajax.request({
  url:'obligation/deleteCustDefaultEvent.do',
  params : {serialNo : grid2.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(response){
	//alert(response.responseText);
	if(response.responseText.indexOf('false')){
		Ext.MessageBox.alert("提示","您无权限删除！");
	}else{
		Ext.MessageBox.alert("提示","删除成功！");
	}
   grid2.store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

function checkDetail(){
if (grid2.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要查看的内容！"); 
}
else{
	var selectedId = grid2.getSelectionModel().getSelections()[0].data.serialNo;
    var status =  grid2.getSelectionModel().getSelections()[0].data.status;     
   	addForm.form.reset(); 
	
   	if(status == '待认定状态' ){
		addWin.buttons[0].handler = function(){Ext.Msg.alert("提示", "当前违约事件已提交，不能修改！");};
	}
	else if(status == '已认定' ){
		addWin.buttons[0].handler = function(){Ext.Msg.alert("提示", "当前违约事件已认定，不能修改！");};
	}
   	else{addWin.buttons[0].handler = edit;}
	 

	Ext.Ajax.request({
		url:'obligation/getCustDefaultEvent.do',
		params : { serialNo: selectedId},
		method:'POST',
  		success:function(response){
		 	var json = Ext.util.JSON.decode(response.responseText);
		 	Ext.getCmp('serialNo').setValue(json.custDE.serialNo);
		 	Ext.getCmp("custId").setValue(json.custDE.custId);
		 	Ext.getCmp("custNo").setValue(json.cust.custNo);
		 	Ext.getCmp("custName").setValue(json.cust.custName);
		 	Ext.getCmp("creditLevelBf").setValue(json.custDE.creditLevelBf);
		  	Ext.getCmp("eventType").setValue(json.custDE.eventType);
			Ext.getCmp("eventDesc").setValue(json.custDE.eventDesc);
		  	Ext.getCmp("eventDate").setValue(json.custDE.eventDate);
		  	Ext.getCmp("capitalValue").setValue(json.custDE.capitalValue);
		  	Ext.getCmp("interestsValue").setValue(json.custDE.interestsValue);
		  	Ext.getCmp("classifyResult").setValue(json.custDE.classifyResult);
		  	Ext.getCmp("classifyDate").setValue(json.custDE.classifyDate);
		  	Ext.getCmp("loanLimit").setValue(json.custDE.loanLimit);
		  	Ext.getCmp("loanBalance").setValue(json.custDE.loanBalance);
		  	Ext.getCmp("eventReason").setValue(json.custDE.eventReason);
			Ext.getCmp("addWin").show(); 
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}

function edit(){
	if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
        url : 'obligation/editCustDefaultEvent.do', 
        method : 'post', 
		success : function() {
		   Ext.getCmp('addWin').hide();
			grid2.store.reload();
			Ext.Msg.alert('提示', '保存成功！'); 
		}, 
        failure : function() { 
			Ext.Msg.alert('提示', '保存失败！'); 
 		} 
	}); 
}
}

grid2.addListener('rowdblclick',checkDetail);

function commit(){
	if (grid2.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
	}
	else{
		var selectedId = grid2.getSelectionModel().getSelections()[0].data.serialNo;
		var status =  grid2.getSelectionModel().getSelections()[0].data.status;
		if(status == '待认定状态' ){
		Ext.Msg.alert("提示", "当前违约事件已提交，不能重复提交！");
	}
	else if(status == '已认定' ){
		Ext.Msg.alert("提示", "当前违约事件已认定！");
	}else{
		Ext.MessageBox.confirm('提示', '确定提交此违约事件吗？', 
			confirm = function(){
			Ext.Ajax.request({
			url:'obligation/commitCustDefaultEvent.do',
			params : { serialNo: selectedId},
			method:'POST',
	  		success:function(response){
	  			window.location.reload();
		  },failure:function(){
			  Ext.MessageBox.alert("复评和审核人员无法提交！");
  			}
			}); 
		});}
}
}

var custId = "";
function listDefault(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
}
else{
	var selectedCustId = grid.getSelectionModel().getSelections()[0].data.custId;
	custId = selectedCustId;
	
	ds2.proxy = new Ext.data.HttpProxy({
		url:'obligation/listCustomerDefaultEvent.do?custId='+selectedCustId,
		method:'POST'
	});
	ds2.load();
	
	Ext.getCmp('win2').show();
}
}


function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedCustId = row.get("custId");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="obligation/showDebtList.do?CustId="+selectedCustId;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
}   
//grid.addListener('rowdblclick',rowdblclickFn);
}); 